Linear Regression

LinearRegression fits a linear model to the dataset by adjusting a set of parameters in order to make the sum of the squared residuals of the model as small as possible

An example

Load diabetes dataset:


In [22]:
import numpy as np
from sklearn import datasets
import matplotlib.pyplot as plt

diabetes = datasets.load_diabetes()
diabetes.data.shape


Out[22]:
(442, 10)

In [23]:
# Use only one feature
diabetes_X = diabetes.data[:, np.newaxis, 2]

## Divide the dataset into train and test set
diabetes_X_train = diabetes_X[:-20]
diabetes_X_test = diabetes_X[-20:]
diabetes_y_train = diabetes.target[:-20]
diabetes_y_test = diabetes.target[-20:]

In [24]:
from sklearn import linear_model
regr = linear_model.LinearRegression()
regr.fit(diabetes_X_train, diabetes_y_train)


Out[24]:
LinearRegression(copy_X=True, fit_intercept=True, n_jobs=1, normalize=False)

In [25]:
# Get parameters
regr.get_params()


Out[25]:
{'copy_X': True, 'fit_intercept': True, 'n_jobs': 1, 'normalize': False}

In [26]:
print(regr.coef_)


[ 938.23786125]

In [27]:
# the mean square error
np.mean((regr.predict(diabetes_X_test)-diabetes_y_test) ** 2)


Out[27]:
2548.0723987259703

In [28]:
# variance score: 1 is perfect prediction, 0 means that there is no linear relationship between X and y
regr.score(diabetes_X_test, diabetes_y_test)


Out[28]:
0.4725754479822713

In [29]:
# Plot output
plt.scatter(diabetes_X_test, diabetes_y_test, color='black')
plt.plot(diabetes_X_test, regr.predict(diabetes_X_test), color = 'blue', linewidth=3)

plt.xticks(())
plt.yticks(())

plt.show()

In [ ]:


In [ ]: